# Stage 1: Build the static files
FROM node:16.15.0-alpine3.15 as frontend-builder
# ARG STAGE
WORKDIR /builder
COPY /frontend/package.json /frontend/package-lock.json ./
RUN npm install
COPY /frontend .
RUN npm run build


# Stage 2: Build the binary
FROM golang:1.19-alpine AS backend-builder

# ARG STAGE
# ARG GOPRIVATE=gitee.com/quantum-force
# ARG GIT_USER
# ARG GIT_TOKEN

WORKDIR /builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
    && apk add --no-cache git \
    && apk add --repository https://mirrors.ustc.edu.cn/alpine/v$(cat /etc/alpine-release | cut -c1-3)/main/ --no-cache --virtual .build-deps \
    build-base ca-certificates
ENV GOPROXY=https://goproxy.cn,direct

# RUN git config --global url."https://$GIT_USER:$GIT_TOKEN@$GOPRIVATE".insteadOf "https://$GOPRIVATE"

COPY Makefile .
COPY go.mod go.sum ./
RUN make download
COPY . .
COPY --from=frontend-builder /builder/build ./frontend/build/
RUN CGO_ENABLED=0 make build


FROM scratch

ARG STAGE

COPY --from=backend-builder /etc/ssl/certs/ca-certificates.crt \
                  /etc/ssl/certs/
COPY --from=backend-builder /builder/conf/config-${STAGE}.toml /etc/config.toml
COPY --from=backend-builder /builder/dist/gscdatasets /bin/

# scf只能监听9000
EXPOSE 3037

ENTRYPOINT ["/bin/gscdatasets", "-c=/etc/config.toml"]
